/* eslint-disable no-unused-vars */

import VectorSource from 'ol/source/Vector';
import GeoJSON from 'ol/format/GeoJSON';
import Overlay from 'ol/Overlay';
// js
import { transformLonlatToNow } from "./util"
import * as turf from '@turf/turf'


// 等值线的tip的值处理
const isoLineNumber = (num, type) => {
  const Kelvin = 273.15
  const obj = {
    temp: (num) => {
      // return (num - Kelvin).toFixed(0) + "℃"
      return (num - Kelvin).toFixed(0)
    },
    sst: (num) => {
      // return num.toFixed(0) + "℃"
      return num.toFixed(0)
    },
    salt: (num) => {
      return num.toFixed(1)
    },
    shww: (num) => {
      // return num.toFixed(1) + "m"
      return num.toFixed(1)
    },
    flow: (num) => {
      // return num.toFixed(1) + "m/s"
      return num.toFixed(1)
    },
  }
  return obj[type](num)
}

export default {
  // 绘制等值面
  drawIsoPolygonFn_old: (data, type) => {
    let coordinates = []
    let tipList = []; // 等值线的tip，用于说明当前等值线的数值
    for (let item of data.features) {
      let list = []
      for (let coors of item.geometry.coordinates[0]) {
        list.push(transformLonlatToNow(coors))
      }
      coordinates.push(list)
      // 生成tip的dom
      // 首先判断等值线面积的大小，不同大小，在不同层级显示
      var polygon = turf.polygon([item.geometry.coordinates[0]]);
      var area = turf.area(polygon) / 1000 / 1000; // 平方千米
      let isOmitted = 5
      if (area < 10000) {
        isOmitted = 7
      } else if (area < 20000) {
        isOmitted = 6
      }
      // 开始生成节点
      let tipElement = document.createElement('div');
      tipElement.className = 'isoLine-tip';
      tipElement.innerHTML = isoLineNumber(item.properties.value, type);
      tipElement.setAttribute('level', isOmitted);
      let measureTooltip = new Overlay({
        element: tipElement,
        offset: [-4, -4],
        positioning: 'bottom-center'
      });
      // 获取tip的坐标，取圈坐标集合的中间经纬度
      tipList.push({
        dom: measureTooltip,
        coor: item.geometry.coordinates[0][Math.floor(item.geometry.coordinates[0].length / 2)], // 取一个中间的经纬度
        isOmitted
      })
      /*************************************** */
      // // 每隔一百个点，记录一次tip
      // const list = item.geometry.coordinates[0];
      // const interval = parseInt(list.length / 500);
      // console.log("interval: ", interval)
      // // 如果是0，就取中间值
      // tipList.push({
      //   dom: measureTooltip,
      //   coor: item.geometry.coordinates[0][Math.floor(item.geometry.coordinates[0].length / 2)], // 取一个中间的经纬度
      // })
      // // 如果不是0，就依次取
      // for (let index = 0; index < interval; ++index) {
      //   tipList.push({
      //     dom: measureTooltip,
      //     coor: item.geometry.coordinates[0][index], // 取一个中间的经纬度
      //   })
      // }
    }
    var geojsonObject = {
      type: 'FeatureCollection',
      features: [
        {
          coordinates: coordinates,
          type: "Polygon"
        }
      ]
    };
    var source = new VectorSource({
      features: (new GeoJSON()).readFeatures(geojsonObject)
    });
    return {
      source,
      tipList,
    }
  },
  drawIsoPolygonFn: (data, type) => {
    let coordinates = []
    let tipList = []; // 等值线的tip，用于说明当前等值线的数值
    for (let item of data.features) {
      let list = []
      if (item.geometry.coordinates[0].length > 50) {
        for (let coors of item.geometry.coordinates[0]) {
          list.push(transformLonlatToNow(coors))
        }
        coordinates.push({
          "type": "Feature",
          "geometry": {
            "type": "LineString",
            "coordinates": list
          },
        })
        // 生成tip的dom
        let tipElement = document.createElement('div');
        tipElement.className = 'isoLine-tip';
        tipElement.innerHTML = isoLineNumber(item.properties.value, type);
        let measureTooltip = new Overlay({
          element: tipElement,
          // offset: [-4, -4],
          offset: [0, 0],
          positioning: 'bottom-center'
        });
        // 获取tip的坐标，取圈坐标集合的中间经纬度
        tipList.push({
          dom: measureTooltip,
          coor: item.geometry.coordinates[0][Math.floor(item.geometry.coordinates[0].length / 2)], // 取一个中间的经纬度
        })
      }
    }
    var geojsonObject = {
      type: 'FeatureCollection',
      features: coordinates
    };
    var source = new VectorSource({
      features: (new GeoJSON()).readFeatures(geojsonObject)
    });
    return {
      source,
      tipList,
    }
  },
  // 绘制等值线
  drawIsoLineFn: (data, type) => {
    let coordinates = []
    let tipList = []; // 等值线的tip，用于说明当前等值线的数值
    for (let item of data.features) {
      let list = []
      for (let coors of item.geometry.coordinates) {
        list.push(transformLonlatToNow(coors))
      }
      coordinates.push({
        "type": "Feature",
        "geometry": {
          "type": "LineString",
          "coordinates": list
        },
      })
      // 生成tip的dom
      let tipElement = document.createElement('div');
      tipElement.className = 'isoLine-tip';
      tipElement.innerHTML = isoLineNumber(item.properties.value, type);
      tipElement.setAttribute('level', 5);
      let measureTooltip = new Overlay({
        element: tipElement,
        offset: [-8, -8],
        positioning: 'bottom-center'
      });
      // 获取tip的坐标，取圈坐标集合的中间经纬度
      console.log("item: ", item)
      tipList.push({
        dom: measureTooltip,
        coor: item.geometry.coordinates[Math.floor(item.geometry.coordinates.length / 2)], // 取一个中间的经纬度
      })
    }
    var geojsonObject = {
      type: 'FeatureCollection',
      features: coordinates
    };
    var source = new VectorSource({
      features: (new GeoJSON()).readFeatures(geojsonObject)
    });
    return {
      source,
      tipList,
    }
  },
}
